package com.heima.admin.service.impl;

import com.heima.model.admin.dto.AdUserDto;
import com.heima.model.admin.entity.AdUser;
import com.heima.admin.dao.AdUserDao;
import com.heima.admin.service.AdUserService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppJwtUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * 管理员用户信息表(AdUser)表服务实现类
 *
 * @author makejava
 * @since 2022-04-22 20:07:57
 */
@Service("adUserService")
public class AdUserServiceImpl implements AdUserService {
    @Resource
    private AdUserDao adUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public AdUser queryById(Long id) {
        return this.adUserDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param adUser 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<AdUser> queryByPage(AdUser adUser, PageRequest pageRequest) {
        long total = this.adUserDao.count(adUser);
        return new PageImpl<>(this.adUserDao.queryAllByLimit(adUser, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param adUser 实例对象
     * @return 实例对象
     */
    @Override
    public AdUser insert(AdUser adUser) {
        this.adUserDao.insert(adUser);
        return adUser;
    }

    /**
     * 修改数据
     *
     * @param adUser 实例对象
     * @return 实例对象
     */
    @Override
    public AdUser update(AdUser adUser) {
        this.adUserDao.update(adUser);
        return this.queryById(adUser.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.adUserDao.deleteById(id) > 0;
    }

    /**
     * 后台登录
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(AdUserDto dto) {
        //1.检查参数
        if (StringUtils.isBlank(dto.getName()) || StringUtils.isBlank(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"用户名或密码为空");
        }
        //2.查询用户
        AdUser adUser = adUserDao.queryByUsername(dto.getName());
        if (adUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户名不存在");
        }
        //3.比对密码
        String salt = adUser.getSalt();
        String password = dto.getPassword();
        String md5Password = DigestUtils.md5DigestAsHex((password + salt).getBytes());
        if (!StringUtils.equals(md5Password, adUser.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //密码正确
        HashMap<String, Object> map = new HashMap<>();
        String token = AppJwtUtil.getToken(adUser.getId());
        adUser.setSalt("");
        adUser.setPassword("");
        map.put("token",token);
        map.put("user",adUser);
        return ResponseResult.okResult(map);

    }
}
